package cc.rengu.igas.channel.upac.core.service.trans;


import cc.rengu.igas.channel.upac.common.constant.UpacParamConstant;
import cc.rengu.igas.channel.upac.common.constant.UpacTreeNodeConstant;
import cc.rengu.igas.channel.upac.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upac.common.enums.UpacProdEnum;
import cc.rengu.igas.channel.upac.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upac.core.service.base.TokenPayService;
import cc.rengu.igas.channel.upac.facade.request.TokenTransStateQueryRequest;
import cc.rengu.igas.channel.upac.facade.response.TokenTransStateQueryRespone;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * 无跳转交易状态查询
 */
public class TokenTransStateQueryService extends TokenPayService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        TokenTransStateQueryRequest tokenTransStateQueryRequest = new TokenTransStateQueryRequest();
        ConvertUtil.convertOutput(tokenTransStateQueryRequest);
        return tokenTransStateQueryRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        String acctEncFlag = xmlTreeUtil.getXmlTreeStringValue(UpacTreeNodeConstant.ACCT_ENC_FLAG);
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        TokenTransStateQueryRespone tokenTransStateQueryRespone = new TokenTransStateQueryRespone();
        TokenTransStateQueryRequest tokenTransStateQueryRequest = (TokenTransStateQueryRequest) request;
        String instId = tokenTransStateQueryRequest.getHeader().getInstId();
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpacTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        ConvertUtil.convertInput(tokenTransStateQueryRequest);
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnType, "00");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnSubType, "00");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.bizType, UpacProdEnum.TOKEN_PAY.getProdCode());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, tokenTransStateQueryRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);

//        rglog.error("调用银联全渠道服务<{}>超时，iReturnCode:<{}>",dstChannelInfo.getDstServiceName(),iReturnCode);
//        tokenTransStateQueryRespone.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
//        tokenTransStateQueryRespone.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
//        chnlBizResponse.setResult(tokenTransStateQueryRespone);
//        return chnlBizResponse;

        if (98 == iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            tokenTransStateQueryRespone.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            tokenTransStateQueryRespone.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            chnlBizResponse.setResult(tokenTransStateQueryRespone);
            return chnlBizResponse;
        } else if (0 != iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(tokenTransStateQueryRespone);
        tokenTransStateQueryRespone.setOrigRespCode(convertRespCode(tokenTransStateQueryRespone.getOrigRespCode()));
        if (UpacParamConstant.SUCCESS_CODE.equals(tokenTransStateQueryRespone.getRespCode()) ||
                UpacParamConstant.DEFECT_SUCCESS_CODE.equals(tokenTransStateQueryRespone.getRespCode())) {
            tokenTransStateQueryRespone.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            tokenTransStateQueryRespone.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        } else {
            tokenTransStateQueryRespone.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            tokenTransStateQueryRespone.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
        }
        if (RespCodeEnum.TRANS_SUCCESS.getRespCode().equals(tokenTransStateQueryRespone.getRespCode())) {
            if (tokenTransStateQueryRespone.getPayAcctNo() != null) {
                if ("Y".equals(acctEncFlag)) {
                    rglog.error("配置账户加密，解密账号");
                    tokenTransStateQueryRespone.setPayAcctNo(descryptData(tokenTransStateQueryRespone.getPayAcctNo()));
                }
            }
        }
        chnlBizResponse.setResult(tokenTransStateQueryRespone);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        TokenTransStateQueryRespone tokenTransStateQueryRespone = (TokenTransStateQueryRespone) response.getResult();
        ConvertUtil.convertInput(tokenTransStateQueryRespone);
    }
}
